シート 2 の範囲内のセル名に基づいてシート (シート 1) を複製して名前を変更するために必要な VBA コード (VBA code needed to duplicate and rename a sheet (Sheet 1) based on cell names in a range on Sheet 2)


問題の説明

シート 2 の範囲内のセル名に基づいてシート (シート 1) を複製して名前を変更するために必要な VBA コード (VBA code needed to duplicate and rename a sheet (Sheet 1) based on cell names in a range on Sheet 2)

このワークブックには、(CAM) と (CAM) の 2 つのワークシートがあります。(DIST)

On (CAM) は、"Distributor" というタイトルのテーブルです。このテーブルに名前を入力すると、(DIST) タブが複製され、シート 1 の "Distributor" テーブルに入力した名前に変更されます。

これは私が使用しようとしたコードですが、 .....私はこれが苦手で、よくわかりません.

Sub AddSheets()

    Dim xRg As Excel.Range
    Dim wSh As Excel.Worksheet
    Dim wBk As Excel.Workbook
   Set wSh = ActiveSheet
    Set wBk = ActiveWorkbook
    Application.ScreenUpdating = False
    For Each xRg In wSh.Range("A1:A7")
    With wBk
        .Sheets.Add after:=.Sheets(.Sheets.Count)
        On Error Resume Next
        ActiveSheet.Name = xRg.Value
        If Err.Number = 1004 Then
          Debug.Print xRg.Value & " already used as a sheet name"
        End If
        On Error GoTo 0
        End With
     Next xRg
    Application.ScreenUpdating = True
 End Sub

リファレンスソリューション

方法 1:

Your program may work as follows: 1. For each entry in cam, check if a coresponding worksheet exists 2. if not, then create the worksheet by duplicating the dist worksheet:

Dim wBk As Workbook
Dim wSh as Worksheet, wNw as Worksheet
Dim xRg as Range
Set wBk = ActiveWorkbook
Set wSh = wBk.Workhseets("CAM")
Set xRg = wSh.Range("B2") ' Assuming your table "CAM" starts in cell B2
while not xRg.Value = ""
   on error resume next
   set wSh = wBk.Worksheets(xRg.Value) ' ‑‑‑ Try accessing worksheet
   on error goto 0                     ' ‑‑‑ Remove error handler
   if wSh is nothing then              ' ‑‑‑ no worksheet
      set sSh = wBk.Worksheets("Dist") ' ‑‑‑ Source worksheet
      set sNw = wBk.Worksheets.Add()   ' ‑‑‑ New worksheet as target
      sSh.UsedRange.Copy               ' ‑‑‑ Copy all from source
      sNw.Range("A1").PasteSpecial xlPasteAll
   end if
wend

Good luck!

(by GunitQuantumRog)

リファレンスドキュメント

  1. VBA code needed to duplicate and rename a sheet (Sheet 1) based on cell names in a range on Sheet 2 (CC BY‑SA 2.5/3.0/4.0)

#vba #excel






関連する質問

2010カスタムリボンにアクセス (access 2010 custom ribbon)

セルに値が保存されているファイルを開く (Open files with values stored in cells)

コンソールはプログラミング言語で制御できますか? (Can consoles be controlled by programming languages?)

このコードを変更して、最後の行の下の行にデータを貼り付けるにはどうすればよいですか? (How do I modify this code to paste in the row under last row with data?)

Selenium-vba クラス名で要素を取得 (Selenium-vba Get element by Class Name)

セルからデータを抽出し、セルをアルファベット順に並べ替えるにはどうすればよいですか? (How do I extract data from a cell and order the cells alphabetically?)

VBAコードがセルをアクティブにしない (VBA code not activating cell)

列をExcel VBAループして、空白以外をコピーして他の3つの列に貼り付けますか? (Excel VBA Loop through Column to Copy and Paste Non Blanks to 3 other Columns?)

コードは 2 つの製品で動作しますが、3 番目の製品コードを追加すると、データが取得されますが、3 番目の製品だけに保存されません。どうしたの? (Code works with two products but when I add a third product code grabs data but doesn't save it only for third product. What's wrong with it?)

シート 2 の範囲内のセル名に基づいてシート (シート 1) を複製して名前を変更するために必要な VBA コード (VBA code needed to duplicate and rename a sheet (Sheet 1) based on cell names in a range on Sheet 2)

signtool.exe エラー: Excel マクロの署名時に SignerSign() が失敗しました (-2147220492/0x800403f4) (signtool.exe Error: SignerSign() failed (-2147220492/0x800403f4) when signing Excel Macro)

Excelで1つの列の「 - 」で区切られたデータを複数に分割する (Divide data separated from ' - ' in one column into more in excel)







コメント